$(function (){

    //获取到当前注册用户的uid
    let href = location.href;
    let uid = href.split("=")[1];
    $(".input-uid").val(uid);

    //将id拼接到跳转的url上
    let $skip = $(".skip");
    let attr = $skip.attr("href");
    $skip.attr("href",attr+"?id="+uid);

    //获取焦点方法
    function focusItem(dom) {
        var eBox = $(dom).next();
        eBox.text("");
    }

    //失去焦点的函数
    function checkItem(dom) {
        var eBox = $(dom).next();
        var name = $(dom).attr("name");
        var value = $(dom).val();
        var errorMessage = "", isError = true;
        switch (name) {
            case "birth":
                if (/^\s*$/.test(value)) {
                    errorMessage = "生日不能为空!";
                }else {
                    isError = false;
                }
                break;
            case "realname":
                if (/^\s*$/.test(value)) {
                    errorMessage = "用户名不能为空!";
                }else if (!/[\u4e00-\u9fa5]{2,6}/.test(value)){
                    errorMessage = "用户真实姓名应在2-6位中文!";
                } else {
                    isError = false;
                }
                break;
            case "nickname":
                if (/^\s*$/.test(value)) {
                    errorMessage = "昵称不能为空!";
                }else if (!/[\u4e00-\u9fa5a-zA-Z0-9]{2,10}/.test(value)){
                    errorMessage = "昵称应在2到10位!";
                } else {
                    isError = false;
                }
                break;
            default:
                isError = false;
                break;
        }
        if (isError) {
            eBox.text(errorMessage);
            return false;
        }
        return true;
    }

    //获得焦点
    $('.upt').focus(function () {
        focusItem(this);
        $("#login-status").text("");
    });

    //失去焦点事件
    $('.upt').blur(function () {
        checkItem(this);
    });


    $(".a-btn").click(function(e){
        e.preventDefault();


        //获取到使用input输入框
        let int = $(this).parents('.box-form').find('.input-t');

        //判断状态,是否有输入框不合法
        let flag = true;
        int.each(function(){
            let zha = checkItem(this);
            if(!zha){
                flag = false;
            }
        });
        let serialize = $("#myform").serialize();

        //合法则提交登录或注册,不合法不做任何操作
        if (flag) {
            $.get(
                "loginServlet/registerTwo",
                $("#myform").serialize(),
                function (data) {
                    console.log(data);
                    if (data.code == 505){
                        alert(data.msg);
                        let obj = data.obj;
                        console.log(obj);
                        location.href = "success.html?id="+obj[0]+"&nickname="+obj[1];
                    }else {
                        alert(data.msg);
                    }
                }
            );
        }
    });

    $("#fileInput").change(function (){
        $(".t1").toggle();
        $(".t2").toggle();
        let formData = new FormData;
        let file = $("#fileInput")[0].files[0];
        formData.append("file",file);
        formData.append("uid",uid);

        $.ajax({
            url:"loginServlet/uploadHead",
            type:"POST",
            data:formData,
            processData: false, // 不处理数据
            contentType: false, // 不设置内容类型
            success:function (data) {
                alert(data.msg);
            }
        });
    });

});